<script lang="ts" setup>
import { ref, watch } from "vue";
import { showSuccessToast } from "vant";
import { useRouter } from "vue-router";
import CreateFrom from "./components/createForm.vue";
import usePost from "@/hooks/usePost";
import { useAppStore } from "@/store/index";
import type { OrderForm } from "./types";

const currUser: any = useAppStore();
const router = useRouter();

const { run: submitRun, data: submitData } = usePost("POST", "/mistake-api/api/tech/pur/insert");

const form = ref<OrderForm>({
	applyerCode: "",
	applyerName: "",
	createUserName: "",
	createUser: "",
	customTime: [],
	fileList: [],
	detailList: [
		{
			patchFlag: "",
			patchFlagText: "",
			repoLink: "",
			tagFlag: "",
			tagFlagText: "",
			remark: "",
			materialName: "",
			materialCode: "",
			testReason: "",
			amount: undefined,
			reason: "",
			specification: "",
			drawCode: ""
		}
	],
	materialCode: "",
	constructNo: "",
	demandDate: "",
	applyReason: "",
	productDest: "",
	attachment: "",
	attachmentPath: "",
	pcbFlag: "",
	pcbFlagText: "",
	applyType: "",
	applyTypeText: "",
	// projecterCode: "admin",
	projecterName: "",
	projectCode: "",
	projectName: "",
	companyCode: "",
	companyName: ""
});

const init = async () => {
	let userId = currUser?.getUserId() || "";
	let userName = currUser?.getUserName() || "";
	form.value.applyerCode = form.value.createUser = userId || "";
	form.value.applyerName = form.value.createUserName = userName || "";
};
init();

const formSubmit = () => {
	submitRun(form.value);
};

watch(
	() => submitData.value,
	() => {
		let val: any = submitData.value;
		if (val && val.code == 200) {
			showSuccessToast({ message: "操作成功", iconSize: 40 });
			setTimeout(() => {
				router.replace("/approveList?type=1&menu=1");
			}, 1500);
		}
	}
);
</script>

<template>
	<div class="pur-index">
		<CreateFrom :form="form" @form-submit="formSubmit" />
	</div>
</template>

<style lang="less" scoped>
.pur-index {
	background-color: #f8f8f8;
}
</style>
